package de.koo.grum.controllers

import de.koo.grum.domain.*
            
class GroupController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        //if(!params.max) params.max = 50
        def groups
        def searchString=""
        
        if(params.searchString) {
        	searchString=params.searchString
        	groups = Group.findAllByNameLike("%${searchString}%")
        }
        else {
        	groups=Group.list(params)
        }
        
        [ groupList: groups, searchString:searchString, resultSize:groups.size(), pageSize:50 ]
    }

    def search = {
            if(!params.max) params.max = 10
            redirect(action:list,[groups:groups,searchString:searchString, resultSize:groups.size(), pageSize:50])
    }

    def show = {
        def group = Group.get( params.id )

        if(!group) {
            flash.message = "Group not found with id ${params.id}"
            redirect(action:list)
        }
        else {
        	def c = User.createCriteria()
        	def users = c.list {
        	       groups {
        	            like('name',group.name)
        	       }
        	}
            return [ group : group, users:users ]
        }
    }

    def delete = {
        def group = Group.get( params.id )
        if(group) {
            group.delete(flush:true)
            flash.message = "Group ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Group not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def group = Group.get( params.id )

        if(!group) {
            flash.message = "Group not found with id ${params.id}"
            redirect(action:list)
        }
        else {
        	def c = User.createCriteria()
	    	def users = c.list {
	 	       groups {
	 	            like('name',group.name)
	 	       }
	    	}
	    	return [ group : group, users:users ]
        }
    }

    def update = {
        def group = Group.get( params.id )
        if(group) {
            group.properties = params
            if(!group.hasErrors() && group.save(flush:true)) {
                flash.message = "Group ${params.id} updated"
                redirect(action:show,id:group.id)
            }
            else {
                render(view:'edit',model:[group:group])
            }
        }
        else {
            flash.message = "Group not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def group = new Group()
        group.properties = params
        return ['group':group]
    }

    def save = {
        def group = new Group(params)
        if(!group.hasErrors() && group.save(flush:true)) {
            flash.message = "Group ${group.id} created"
            redirect(action:show,id:group.id)
        }
        else {
            render(view:'create',model:[group:group])
        }
    }

    def addUsersToGroup = {
        def group = Group.get( params.id )

        if(!group) {
            flash.message = "Group not found with id ${params.id}"
            redirect(action:list)
        }
        else {
        	def c = User.createCriteria()
	    	def users = c.list {
	 	       groups {
	 	            like('name',group.name)
	 	       }
	    	}
        	def availableUsers=User.list() - users
	    	return [ group : group, users:users , availableUsers:availableUsers ]
        }
    }

    def updateGroupUsers = {
        def group = Group.get( params.id )
        def users = []

        if(params.Group){
	        if (params.Group.users instanceof java.lang.String) {
	        	users[0]=User.get(params.Group.users.toInteger())        	
			} else {
	        	users=User.getAll(params.Group.users*.toInteger())
			}
        }
        group.users=users
        
        if(!group.hasErrors() && group.save(flush:true)) {
            flash.message = "Users added to or removed from group ${group.name} successfully."
            redirect(action:edit,id:group.id)
        }
        else {
            redirect(action:addUsersToGroup,id:group.id)
        }
    }
}